using System;
using System.Net;
using System.Net.Sockets;

namespace Fusion.Net.UDP
{
    public class UDPClientSocket : UDPSocket
    {
        public UDPClientSocket(UDPNet udp, AddressFamily addressFamily) : base(udp, addressFamily)
        {
        }

        public UDPClientSocket(UDPNet udp, Socket socket) : base(udp, socket)
        {
        }

        protected override void Invoke()
        {
            EndPoint receiveIP = new IPEndPoint(Socket.AddressFamily == AddressFamily.InterNetworkV6 ? IPAddress.IPv6Any : IPAddress.Any, 0);
            int length = Socket.ReceiveFrom(buffer, ref receiveIP);
            byte[] data = new byte[length];
            Array.Copy(buffer, 0, data, 0, length);
            UDPClientSocket socket = new UDPClientSocket(udp, null);
            socket.Bind(receiveIP);
            netBase.DataHandler.Enqueue(socket, new NetDataHandler.DataWrap(data));
        }
    }
}
